package com.DreamTeam.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import com.DreamTeam.dao.IProductDao;
import com.DreamTeam.po.Employee;
import com.DreamTeam.po.Product;

public class ProductDao implements IProductDao {
	private Connection conn = null;
	
	public ProductDao(Connection conn){
		this.conn = conn;
	}
	@Override
	public boolean doAdd(Product product) {
		// TODO Auto-generated method stub
		String sql = "insert into product values(?,?,?,?)";
		PreparedStatement prsmt = null;
		int flag= 0;
		try{
			prsmt = conn.prepareStatement(sql);
			prsmt.setString(1,product.getProductNo());
			prsmt.setString(2,product.getProductName());
			prsmt.setString(3,product.getProductClass());
			prsmt.setDouble(4,product.getProductPrice());
			flag = prsmt.executeUpdate();
		}catch(SQLException e){
			e.printStackTrace();
		}
		if(flag > 0)
			return true;
		return false;
	}

	@Override
	public boolean doUpdate(Product product) {
		// TODO Auto-generated method stub
		String sql = "update product set ProductNo=?,ProductName =?, productClass =?,ProductPrice =? where ProductNo= ?";
		PreparedStatement prsmt = null;
		int flag = 0;
		try{
			prsmt = conn.prepareStatement(sql);
			prsmt.setString(1,product.getProductNo());
			prsmt.setString(2,product.getProductName());
			prsmt.setString(3,product.getProductClass());
			prsmt.setDouble(4,product.getProductPrice());
			prsmt.setString(5,product.getProductNo());
			flag = prsmt.executeUpdate();
		}catch(SQLException e){
			e.printStackTrace();
		}
		if(flag > 0)
			return true;
		return false;
	}

	@Override
	public boolean doDelete(Product product) {
		// TODO Auto-generated method stub
		String sql = "delete from product where ProductNo =?";
		PreparedStatement prsmt = null;
		int flag = 0;
		try{
			prsmt = conn.prepareStatement(sql);
			prsmt.setString(1,product.getProductNo());
			flag =prsmt.executeUpdate();
		}catch(SQLException  e){
			e.printStackTrace();
		}
		if(flag > 0)
			return true;
		return false;
	}

 
	@Override
	public List<Product> queryProduceByColumn(String keyword,String columnName,String orderColumn,String model) {
		// TODO Auto-generated method stub
		String sql = "select * from product where "+columnName+" like ? order by "+orderColumn+" "+model;
		PreparedStatement prsmt = null;
		List<Product> list = new ArrayList();
		try{
			prsmt = conn.prepareStatement(sql);
			prsmt.setString(1, "%"+keyword+"%");
			ResultSet re = prsmt.executeQuery();
			while(re.next()){
				Product product = new Product();
				product.setProductNo(re.getString(1));
				product.setProductName(re.getString(2));
				product.setProductClass(re.getString(3));
				product.setProductPrice(re.getDouble(4));
				list.add(product);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		return list;
	}
	
	@Override
	public List<Product> queryProductAll() {
		String sql = "select * from product";
		List<Product> list = new ArrayList();
		PreparedStatement prsmt = null;
		try {
			prsmt = conn.prepareStatement(sql);
			ResultSet re = prsmt.executeQuery();
			while(re.next()){
				Product product = new Product();
				product.setProductNo(re.getString(1));
				product.setProductName(re.getString(2));
				product.setProductClass(re.getString(3));
				product.setProductPrice(re.getDouble(4));
				list.add(product);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	@Override
	public List<Product> queryOrderByColumnName(String columnName,String model) {
		String sql = "select * from product order by "+columnName+" "+model;
		List<Product> list = new ArrayList();
		PreparedStatement prsmt = null;
		try {
			prsmt = conn.prepareStatement(sql);
			ResultSet re = prsmt.executeQuery();
			while(re.next()){
				Product product = new Product();
				product.setProductNo(re.getString(1));
				product.setProductName(re.getString(2));
				product.setProductClass(re.getString(3));
				product.setProductPrice(re.getDouble(4));
				list.add(product);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	@Override
	public List<Product> queryByKeyword(String keyword) {
		String sql = "select * from product where productNo like ? or productName like ? " +
				"or productClass like ? or productPrice like ?";
		List<Product> list = new ArrayList();
		PreparedStatement prsmt = null;
		try {
			prsmt = conn.prepareStatement(sql);
			prsmt.setString(1, "%"+keyword+"%");
			prsmt.setString(2, "%"+keyword+"%");
			prsmt.setString(3, "%"+keyword+"%");
			prsmt.setString(4, "%"+keyword+"%");
			ResultSet re = prsmt.executeQuery();
			while(re.next()){
				Product product = new Product();
				product.setProductNo(re.getString(1));
				product.setProductName(re.getString(2));
				product.setProductClass(re.getString(3));
				product.setProductPrice(re.getDouble(4));
				list.add(product);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	
	
}
